//
//  String+Extensions.swift
//  MYModule
//
//  Created by 李然 on 2022/7/20.
//

import Foundation
import CommonCrypto

//MARK: - 通用
public extension String {
    
    var url: URL? {
        return URL(string: self)
    }
    
    static func className(_ aClass: AnyClass) -> String {
        return NSStringFromClass(aClass).components(separatedBy: ".").last!
    }
    
}


//MARK: - 国际化
public extension String {
    
    /// 国际化(一般调用这个方法)
    var language : String {
        
       return language()
    }
    
    /// 国际化
    func language( _ comment : String = "", bundle: Bundle = Bundle.main) -> String {
        NSLocalizedString(self, bundle: bundle, comment: comment)
    }
    
    /// 格式化拼接国际化
    func language(with arguments: [CVarArg], locale: Locale? = nil, comment : String = "", bundle: Bundle = Bundle.main) -> String {
        let language = self.language(comment, bundle: bundle)
        return String(format: language, locale: locale, arguments: arguments)
    }
}

//MARK: - 宽高计算
public extension String {
    
    /// 计算文字的高度
    /// - Parameters:
    ///   - content: content
    ///   - font: font
    ///   - width: 需要显示的宽度
    /// - Returns: 文字高度
    func calculateHeight(font: UIFont, width: CGFloat) -> CGFloat {
        let text = NSString(string: self)
        // 配置文本绘制选项
        let options = NSStringDrawingOptions.usesLineFragmentOrigin
        // 配置计算属性参数
        let atrributes = [NSAttributedString.Key.font: font]
        let size = CGSize(width: width, height: CGFloat(MAXFLOAT))
        let rect = text.boundingRect(with: size,
                                     options: options,
                                     attributes: atrributes,
                                     context: nil)
        return rect.height
    }
    
    /// 计算文字的宽度
    /// - Parameters:
    ///   - content: content
    ///   - font: font
    ///   - height: 需要显示的高度
    /// - Returns: 文字宽度
    func calculateWidth(font: UIFont, height: CGFloat) -> CGFloat {
       
        // 配置计算属性参数
        let size = CGSize(width: CGFloat(MAXFLOAT), height: height)
        let rect = self.boundingRect(with: size,
                                     options: [.usesLineFragmentOrigin, .usesFontLeading],
                                     attributes: [.font: font],
                                     context: nil)
        return rect.width
    }
    
    /// 计算一行显示的文字的宽度
    /// - Parameters:
    ///   - content: content
    ///   - font: font
    /// - Returns: 宽度
    func calculateOneLineWidth(font: UIFont) -> CGFloat {
        let text = NSString(string: self)
        // 配置文本绘制选项
        let options = NSStringDrawingOptions.truncatesLastVisibleLine
        // 配置计算属性参数
        let atrributes = [NSAttributedString.Key.font: font]
        let rect = text.boundingRect(with: CGSize(width: NSIntegerMax,
                                                  height: NSIntegerMax),
                                     options: options,
                                     attributes: atrributes,
                                     context: nil)
        return rect.width
    }
    
}

public extension String {
    ///（如果backwards参数设置为true，则返回最后出现的位置）
    func positionOf(sub:String, backwards:Bool = false)->Int {
        // 如果没有找到就返回-1
        var pos = -1
        if let range = range(of:sub, options: backwards ? .backwards : .literal ) {
            if !range.isEmpty {
                pos = self.distance(from:startIndex, to:range.lowerBound)
            }
        }
        return pos
    }
}
